import 'dart:convert';

import 'package:PollOnLine/ui/home/signature/signature_view_ipad.dart';
import 'package:PollOnLine/ui/home/task_detail/task_detail_logic.dart';
import 'package:PollOnLine/utils/logger_utils.dart';
import 'package:PollOnLine/utils/toast_utils.dart';
import 'package:PollOnLine/utils/utils.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;

import '../../../bean/FileRes.dart';
import '../../../bean/request/SaveTaskFile.dart';
import '../../../network/network.dart';
import '../../../widget/ImagesGridviewWidget.dart';
import '../signature/signature_view.dart';
import 'complete_poll_state.dart';

class CompletePollLogic extends GetxController {
  final CompletePollState state = CompletePollState();

  final TaskDetailLogic taskDetailLogic = Get.find<TaskDetailLogic>();

  late String writeId; // 问卷ID

  @override
  void onInit() {
    super.onInit();
    // Map<String, dynamic> params = {
    //   "writeId": writeId,
    //   "isSignature": taskDetailLogic.state.isSignature,
    //   "isVideo": taskDetailLogic.state.isVideo,
    // };
    writeId = Get.arguments['writeId'];
    state.isSignature = Get.arguments['isSignature'];
    state.isVideo = Get.arguments['isVideo'];
  }

  @override
  void onReady() {
    super.onReady();
  }

  void goToSignature() async {
    dynamic value;
    // 跳转新增样本
    if (isPad(Get.context!)) {
      value = await Get.to(() => const SignaturePageIpad(title: '电子签名'), arguments: writeId);
    } else {
      value = await Get.to(() => const SignaturePage(title: '电子签名'), arguments: writeId);
    }
    if (value != null) {
      state.image = value;
      update();
      // 当不需要照片时  自动提交
      if (!state.isVideo) {
        taskDetailLogic.completeWrite(writeId);
      }
    }
  }

  void complete() async {
    if (state.isSignature && state.image.isEmpty) {
      toast.show("电子签名不能为空");
      return;
    }
    if (state.isVideo) {
      if (state.imagesList.isEmpty) {
        toast.show("照片不能为空");
        return;
      } else {
        await uploadFiles(state.imagesList);
      }
    }
    taskDetailLogic.completeWrite(writeId);
  }

  Future<void> uploadFiles(List<Media> list) async {
    List<dio.MultipartFile> multipartFiles = [];
    for (Media media in list) {
      if (media.path != null && media.path != '' && media.name != null) {
        multipartFiles.add(dio.MultipartFile.fromFileSync(media.path!, filename: media.name));
      }
    }
    ResultData resultData = await Api.uploadFiles(multipartFiles);
    if (resultData.isSuccess) {
      List<dynamic> list = jsonDecode(jsonEncode(resultData.data['data']));
      List<FileRes> fileResList = list.map((e) => FileRes.fromJson(e)).toList();
      await saveTaskFile(fileResList);
    } else {
      toast.show("文件上传失败");
    }
  }

  // 保存任务文件信息
  Future<void> saveTaskFile(List<FileRes> fileResList) async {
    SaveTaskFile saveTaskFile = SaveTaskFile();
    saveTaskFile.busType = BUS_TYPE_PHOTO_VIDEO;
    List<FileRes> list = fileResList;
    saveTaskFile.fileShowList = list;
    saveTaskFile.writeId = writeId;

    ResultData resultData = await Api.saveTaskFile(saveTaskFile.toJson());
    if (resultData.isSuccess) {
      toast.show("文件上传成功");
    }
  }

  @override
  void onClose() {
    super.onClose();
  }
}
